# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2023/11/26
desc: 
sample: 
"""
import random

from faker import Faker

fake = Faker('zh_CN')
import pymysql.cursors


connection = pymysql.connect(host='192.168.79.170',
                             port=3307,
                             user='root',
                             password='123456',
                             db='exam_db')
cursor = connection.cursor()

cursor.execute("SELECT id FROM subject")
#fetchall() fetchone  fetchmany
rows = cursor.fetchall()
# ids =[]
# for item in rows:
#     ids.append(item[0])

# def getId(item):
#     return item[0]
ids = list(map(lambda x:x[0],rows))


student_sql ='INSERT INTO student(id,subjectId,name,password,trueName,tel,createTime) VALUES (%s,%s,%s,%s,%s,%s,%s)'
#构造两万数据
data=[]
for i in range(20000):
    create_time = fake.date_time_between(start_date="-2y", end_date="now")
    str_random_date = create_time.strftime('%Y-%m-%d %H:%M:%S')
    print(str_random_date)
    subject = random.choice(ids)
    data.append((i+1,subject,'test'+str(i),'123456',fake.name(),fake.phone_number(),str_random_date))

print(data)
try:
    cursor.execute("delete from student")
    # execute executemany(批量）
    cursor.executemany(student_sql,data)
    connection.commit()
except:
    connection.rollback()
finally:
    connection.close()